package com.compayname.projectname.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.compayname.projectname.config.InstallMainConfigs;
import com.compayname.projectname.consts.Constants;
import com.compayname.projectname.controller.data.InstallaMaintenData;
import com.compayname.projectname.data.DeviceReq;
import com.compayname.projectname.data.InstallmaintainReq;
import com.compayname.projectname.data.loginReq;
import com.compayname.projectname.util.HttpClientUtils;
import org.junit.platform.commons.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class InterService {

    private static Logger logger = LoggerFactory.getLogger(InterService.class);

    @Autowired
    private InstallMainConfigs installMainConfigs;

    public String getToken(String loginInfo) {
        String accessToken = "";

        Map<String, String> headMap = new HashMap<String, String>();
        loginReq paramsData = new loginReq();
        paramsData.setLoginInfo(loginInfo);
        String params = JSON.toJSONString(paramsData);

        String result = "";
        try {
            String httpUrl = "http://" + installMainConfigs.getServerHost() + ":" +  installMainConfigs.getServerPost() + Constants.LOGIN_URL;
            logger.debug("==> Provider - getToken() - POST:{}", httpUrl);
            logger.debug("==> Provider - getToken() - PARAMS:{}", params);
            result = HttpClientUtils.sendHttpPost(Constants.CONTENT_TYPE_JSON, httpUrl, params, headMap);
            logger.debug("==> Provider - getToken() - result:{}", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            String iResult = jsonObject.getString("code");
            if ("1".equals(iResult)) {
                String resultStr = jsonObject.getString("result");
                JSONObject dataObject = JSONObject.parseObject(resultStr);
                accessToken = dataObject.getString("token");
            }
        }
        return accessToken;
    }

    public String getInstallmaintainList(InstallmaintainReq paramsData, String token) {
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Authorization", "dghy_token " + token);
        String params = JSON.toJSONString(paramsData);

        String result = "";
        try {
            String httpUrl = "http://" + installMainConfigs.getServerHost() + ":" +  installMainConfigs.getServerPost() + Constants.INSTALL_PAGE_LIST_URL;
            logger.debug("==> Provider - getInstallmaintainList() - POST:{}", httpUrl);
            logger.debug("==> Provider - getInstallmaintainList() - PARAMS:{}", params);
            result = HttpClientUtils.sendHttpPost(Constants.CONTENT_TYPE_JSON, httpUrl, params, headMap);
            logger.debug("==> Provider - getInstallmaintainList() - result:{}", result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("------------------------------getInstallmaintainList接口异常:" + e.getMessage());
        }
        return result;
    }

    public String getDeviceInfo(DeviceReq paramsData, String token) {
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Authorization", "dghy_token " + token);
        String params = JSON.toJSONString(paramsData);

        String result = "";
        try {
            String httpUrl = "http://" + installMainConfigs.getServerHost() + ":" +  installMainConfigs.getServerPost() + Constants.EQPTINFO_URL;
            logger.debug("==> Provider - getDeviceInfo() - POST:{}", httpUrl);
            logger.debug("==> Provider - getDeviceInfo() - PARAMS:{}", params);
            result = HttpClientUtils.sendHttpPost(Constants.CONTENT_TYPE_JSON, httpUrl, params, headMap);
            logger.debug("==> Provider - getDeviceInfo() - result:{}", result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("------------------------------getDeviceInfo接口异常:" + e.getMessage());
        }
        return result;
    }

    public String getCameraList(DeviceReq paramsData, String token) {
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Authorization", "dghy_token " + token);
        String params = JSON.toJSONString(paramsData);

        String result = "";
        try {
            String httpUrl = "http://" + installMainConfigs.getServerHost() + ":" +  installMainConfigs.getServerPost() + Constants.EQPTINFO_LIST_URL;
            logger.debug("==> Provider - getCameraList() - POST:{}", httpUrl);
            logger.debug("==> Provider - getCameraList() - PARAMS:{}", params);
            result = HttpClientUtils.sendHttpPost(Constants.CONTENT_TYPE_JSON, httpUrl, params, headMap);
            logger.debug("==> Provider - getCameraList() - result:{}", result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("------------------------------getCameraList接口异常:" + e.getMessage());
        }
        return result;
    }

    public String getCameraInfo(DeviceReq paramsData, String token) {
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Authorization", "dghy_token " + token);
        String params = JSON.toJSONString(paramsData);

        String result = "";
        try {
            String httpUrl = "http://" + installMainConfigs.getServerHost() + ":" +  installMainConfigs.getServerPost() + Constants.CAMERA_URL;
            logger.debug("==> Provider - getCameraInfo() - POST:{}", httpUrl);
            logger.debug("==> Provider - getCameraInfo() - PARAMS:{}", params);
            result = HttpClientUtils.sendHttpPost(Constants.CONTENT_TYPE_JSON, httpUrl, params, headMap);
            logger.debug("==> Provider - getCameraInfo() - result:{}", result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("------------------------------getCameraInfo接口异常:" + e.getMessage());
        }
        return result;
    }

    public String getSIPServiceInfo(DeviceReq paramsData, String token) {
        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Authorization", "dghy_token " + token);
        String params = JSON.toJSONString(paramsData);

        String result = "";
        try {
            String httpUrl = "http://" + installMainConfigs.getServerHost() + ":" +  installMainConfigs.getServerPost() + Constants.SIP_SERVICE_URL;
            logger.debug("==> Provider - getSIPServiceInfo() - POST:{}", httpUrl);
            logger.debug("==> Provider - getSIPServiceInfo() - PARAMS:{}", params);
            result = HttpClientUtils.sendHttpPost(Constants.CONTENT_TYPE_JSON, httpUrl, params, headMap);
            logger.debug("==> Provider - getSIPServiceInfo() - result:{}", result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("------------------------------getSIPServiceInfo接口异常:" + e.getMessage());
        }
        return result;
    }

}
